﻿CloudDisk.ListView = function(cfg){
    CloudDisk.AbstractView.apply(this, [cfg]);
    this.el = $('<dl class="list-view"></dl>').appendTo(cfg.ct);
};

CloudDisk.ListView.prototype = {
    itemSelector: '.list-view-item',	
		
	createBlankItem: function(){
	    var item = $('<dd class="list-view-item"><input type="text" value="新建文件夹"></dd>');
		this.el.prepend(item);
		item.find('input').on('blur', function(){
		    item.remove();
		}).select();	    
	},
	
	createItem: function(data){
	    var item = $('<dd class="list-view-item"></dd>');
		var that = this;
		item.html(this.itemTpl.render(data));
		item.attr('itemId', data.file_id);

		if(data.selected){
		    item.addClass('selected');
		}
		
		item.on('click', function(e){
		    var tar = $(e.target);
			var itemId = $(this).attr('itemId');
			if(tar.hasClass('file-name')){
		        that.fireEvent('open', e, itemId);
			} else if(tar.hasClass('icon-share')){
		        that.fireEvent('share', e, itemId);
			} else if(tar.hasClass('icon-favorite')){
			    that.fireEvent('favorite', e, itemId);
			} else if(tar.hasClass('icon-delete')){
			    that.fireEvent('delete', e, itemId);
			} else {
				if($(this).hasClass('selected')){
					$(this).removeClass('selected');
					that.fireEvent('unselect', e, $(this).attr('itemId'));
				} else {
					$(this).addClass('selected');
					that.fireEvent('select', e, $(this).attr('itemId'));
				}
			}
		});
	    return item;
	},
	
	onItemClick: function(item, e){
	    var tar = $(e.target);
	    var that = $(item);
		
		if(tar.hasClass('icon-share')){
		
		} else if(tar.hasClass('icon-favorite')){
		} else if(tar.hasClass('icon-delete')){
		} else {
			if(that.hasClass('selected')){
				that.removeClass('selected');
			} else {
				that.addClass('selected');
			}
		}
	}

};
CloudDisk.Util.extend(CloudDisk.ListView, CloudDisk.AbstractView);